<template>
  <div>
      <common-head title="登录中心" />
    <div class="box">
      <van-form @submit="onSubmit">
        <van-field
          v-model="userInfo.username"
          name="username"
          label="会员名称"
          placeholder="会员名/已绑定手机号"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="userInfo.pwd"
          type="password"
          name="pwd"
          label="会员密码"
          placeholder="会员密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit"
            >登录</van-button
          >
        </div>
      </van-form>
      <van-button @click="$router.push('/register')">注册</van-button>
    </div>
  </div>
</template>

<script>
import CommonHead from '../../components/CommonHead.vue'
import { doLogin } from 'api'
import { Toast } from 'vant'
export default {
  name: 'login',
  data () {
    return {
      userInfo: {
        username: '',
        pwd: ''
      }
    }
  },
  methods: {
    onSubmit (params) {
      params = {
        ...params,
        deviceId: 1111,
        deviceName: 'dhwidw'
      }
      doLogin(params).then((res) => {
        if (res.data.code === 0) {
          localStorage.setItem('token', res.data.data.token)
          Toast.success({
            message: '登录成功',
            duration: 1000,
            onClose: () => {
              // 跳转到其他页面
              if (this.$route.query.from) {
                this.$router.replace(this.$route.query.from)
              } else {
                this.$router.replace('/home')
              }
            }
          })
        } else {
          Toast.fail('登录失败')
        }
      })
    }
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.box {
  margin-top: 70px;
}
/deep/.van-nav-bar__content {
  background-color: #00a236;
}
/deep/.van-nav-bar .van-icon {
  color:#fff
}
/deep/.van-nav-bar__title{
  color: #fff;
  font-size: 17px;
}
.van-button--default{
  width: 92%;
  margin: 0 15px;
  border:1px solid #00a236;
  border-radius: 20px;
  color: #fff;
}
.van-button--default[data-v-aeba673c]{
 background-color: #F39800;
 border-color: #F39800;
}
/deep/.van-field__label {
  font-size: 16px;
}
/deep/.van-field__control {
  font-size: 15px;
}
.van-button--info {
  border-color:#00a236 ;
}
.van-button--info[data-v-aeba673c] {
  background-color: #00a236;
}
</style>
